App.controller('templateController', ['$rootScope', '$scope', '$state', 'Page', function ($rootScope, $scope, $state, Page) {

    $scope.templateGrid = {
        column_defs: [
            {
                target: 3,
                buttons: [
                    {
                        name: '编辑',
                        clazz: 'btn-edit',
                        icon: 'fa-edit',
                        event: function(row){
                            $scope.template = row;
                            $scope.templateAddModal.show();
                        }
                    },
                    {
                        name: '删除',
                        clazz: 'btn btn-delete',
                        icon: 'fa-trash-o',
                        event: function(row){
                            Page.confirm("您确定要删除吗?", function () {
                                Page.ajaxPost('template/delete.do', row, function () {
                                    $scope.templateGrid.query();
                                });
                            });
                        }
                    }
                ]
            }
        ],
        add : function() {
            $scope.template = {};
            $scope.templateAddModal.show();
        }
    }

    $scope.templateAddModal = {
        onShow : function() {
            if($scope.template.templateId) {
                this.setTitle("编辑指标");
            } else {
                this.setTitle("新增指标");
            }
        },
        onOkClick : function() {
            if(!$scope.validateForm.$valid) {
                $scope.validateForm.doValidate();
            } else {
                if(!$scope.template.templateId) { //添加
                    Page.ajaxPost('template/add.do', $scope.template, function (resp) {
                        $scope.templateGrid.query();
                        $scope.templateAddModal.hide();
                    });
                } else {
                    Page.ajaxPost('template/update.do', $scope.template, function (resp) {
                        $scope.templateGrid.query();
                        $scope.templateAddModal.hide();
                    });
                }
            }
        },
        onHidden : function() {
            $scope.validateForm.reset();
        }
    }
}]);
